package com.wakala.employment.fragment.profile;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.leaf.library.StatusBarUtil;
import com.wakala.employment.R;
import com.wakala.employment.adapter.bean.TabEntity;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.databinding.FragmentBasicsInfoBinding;
import com.wakala.employment.databinding.FragmentFindJobInfoBinding;
import com.wakala.employment.fragment.finder.JobCategoryFragment;
import com.wakala.employment.fragment.other.SimpleKindFragment;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xutil.common.StringUtils;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FindJobInfoFragment#} factory method to
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class FindJobInfoFragment extends BaseFragment<FragmentFindJobInfoBinding> implements View.OnClickListener {

    //用户选择的工种
    private ArrayList<String> tagLayoutTags = new ArrayList<>();

    SlidingTabLayout tabLayoutKinds = null;
    private CommonTabLayout tabLayoutKinds2 = null;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private int[] mIconUnselectIds = {
            R.drawable.icon_star, R.drawable.icon_star,
            R.drawable.icon_star, R.drawable.icon_star};
    private int[] mIconSelectIds = {
            R.drawable.icon_star_fill_king, R.drawable.icon_star_fill_king,
            R.drawable.icon_star_fill_king, R.drawable.icon_star_fill_king};
    private final String[] mTitles = {
            "热门", "前端", "后端", "设计"
    };
    private MyPagerAdapter mAdapter;

    @NonNull
    @Override
    protected FragmentFindJobInfoBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentFindJobInfoBinding.inflate(inflater, container, attachToRoot);
    }

    /**
     * @return 返回为 null意为不需要导航栏
     */
    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle();
        titleBar.setTitle("找活信息");
        titleBar.setTitleColor(R.color.black);
        titleBar.setLeftTextColor(R.color.black);
        titleBar.setLeftImageDrawable(getResources().getDrawable(R.drawable.icon_arrow_back));
        titleBar.setBackgroundResource(R.color.white);//设置标题栏背景颜色
        StatusBarUtil.setDarkMode(getActivity());
        //设置状态栏背景颜色，如果设置为 Color.TRANSPARENT 将会隐藏状态栏
        getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.white));
        return titleBar;
    }

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Intent data) {
        //super.onFragmentResult(requestCode, resultCode, data);
        if(resultCode==100){
            //工种分类选择返回数据
            tagLayoutTags=data.getStringArrayListExtra("tagLayoutTags");
            String kindText = "";
            for(String tagText:tagLayoutTags){
                kindText+=tagText+"、";
            }
            kindText = StringUtils.cutString(kindText,0,kindText.length()-1);
            binding.stvModifyKind.setCenterString(kindText);
        }else if(resultCode==101){
            int s = resultCode;
        }
    }

    @Override
    protected void initListeners() {
        binding.stvModifyKind.setOnClickListener(this);
    }

    @Override
    protected void initViews() {
        for (String title : mTitles) {
            mFragments.add(SimpleKindFragment.getInstance(title));
        }
        View decorView = getActivity().getWindow().getDecorView();
        ViewPager vp = findViewById(R.id.vp);
        mAdapter = new MyPagerAdapter(getFragmentManager());
        vp.setAdapter(mAdapter);
        tabLayoutKinds = binding.tlWorkKind;
        tabLayoutKinds.setViewPager(vp,mTitles);
        tabLayoutKinds.setTabPadding(5);
        tabLayoutKinds.setHorizontalScrollBarEnabled(true);

        //
        tabLayoutKinds2 = binding.tlWorkKind2;
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        tabLayoutKinds2.setTabData(mTabEntities);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.stv_modify_kind){
            openPageForResult(JobCategoryFragment.class,100);
        }
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

}